﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Septagon.TwochConnecter.Data.Responses;
using Septagon.TwochConnecter.Data.Threads;

namespace Septagon.TwochConnecter.Data.Bbses
{
	internal class TwochConfig : IBbsConfig
	{
		public virtual Func<string, Res> AnalysisFunction
		{
			get
			{
				return new Func<string, Res>(res =>
				{
					return new Res();
				});
			}
		}

		public Regex AnalysisRegex
		{
			get
			{
				throw new NotImplementedException();
			}
		}

		public Regex AnalysisThreadListRegex
		{
			get
			{
				throw new NotImplementedException();
			}

		}

		public virtual Func<string, ThreadCollection> AnalysisThreadListFunction
		{
			get
			{
				return new Func<string, ThreadCollection>((list) =>
				{
<<<<<<< HEAD
					var threadCol = new ThreadCollection();
=======
					var tc = new ThreadCollection();
>>>>>>> origin/master
					foreach (var item in list.Split('\n'))
					{
						string[] spl = item.Split(new string[] { "<>" }, StringSplitOptions.RemoveEmptyEntries);
						var thread = new Thread();
						thread.ThreadKey = spl[0].Replace(".dat", "");
<<<<<<< HEAD
						string[] spl2 = spl[1].Split(' ');
						var title = "";
						var count = "";
						if (spl2.Length > 2)
						{
							for (int i = 0; i < spl.Length - 1; i++)
							{
								title += spl[i];
							}
							count = spl[spl.Length - 1];
						}
						else
						{
							title = spl2[0];
							count = spl[1];
						}
						thread.ThreadName = title;
						thread.ResCount = count;
						threadCol.Add(thread);

					}
					return threadCol;
=======
						int lastStart = spl[1].LastIndexOf('(');
						string countsText = spl[1].Substring(lastStart);
						string title = spl[1].Remove(lastStart - 1);
						thread.ThreadName = title;
						countsText = countsText.Replace("(", "").Replace(")", "");
						thread.ResCount = countsText;
						tc.Add(thread);
					}
					return tc;
>>>>>>> origin/master
				});
			}

		}

		public virtual string BbsTopPageUrl
		{
			get
			{
				return "http://www.2ch.net/";
			}
		}

		public bool IsRegexResAnalysis
		{
			get
			{
				return false;
			}
		}

		public bool IsRegexThreadAnalysis
		{
			get
			{
				return false;
			}

		}

		public virtual string LocalRulePageUrl
		{
			get
			{
				return "http://{host}/{folder}/head.txt";
			}

		}

		public virtual string SettingTxtPath
		{
			get
			{
				return "http://{host}/{folder}/SETTING.TXT";
			}
		}

		public virtual string ThreadListReadBaseUrl
		{
			get
			{
				return "http://{host}/{folder}/subject.txt";
			}
		}

		public virtual string ThreadReadBaseUrl
		{
			get
			{
				return "http://{host}/{folder}/dat/{key}.dat";
			}
		}

		public class TwochUrlParser : IUrlParser
		{

		}
	}
}
